package tests;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import junit.framework.Assert;

import model.Belegung;
import model.Kunde;
import model.Rechnung;
import model.Reservierung;
import model.Zimmer;

import org.eclipse.persistence.annotations.AdditionalCriteria;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import controller.PersistenceManager;

public class TC_PersistanceManager_001
{
	private PersistenceManager pm = PersistenceManager.getInstance();

	@Before
	public void init()
	{
		this.pm.clearModel();
		// ###### Kunden

		Kunde k0 = new Kunde("parent0", "nach0", 0, new Date("1 Jan 1900")); // parent
		Kunde k1 = new Kunde("parent1", "nach1", 0, new Date("1 Jan 1900")); // parent
		Kunde k2 = new Kunde("parent2", "nach2", 0, new Date("1 Jan 1900")); // parent
		Kunde k3 = new Kunde("parent3", "nach3", 0, new Date("1 Jan 1900")); // parent
		Kunde k4 = new Kunde("child4", "nach4", 0, new Date("1 Jan 2010")); // child
		Kunde k5 = new Kunde("child5", "nach5", 0, new Date("1 Jan 2010")); // child
		pm.saveEntity(k0);
		pm.saveEntity(k1);
		pm.saveEntity(k2);
		pm.saveEntity(k3);
		pm.saveEntity(k4);
		pm.saveEntity(k5);

		// ###### Kunden

		Zimmer z0 = new Zimmer("zimmer0", 1);
		Zimmer z1 = new Zimmer("zimmer1", 2);
		Zimmer z2 = new Zimmer("zimmer2", 3);
		Zimmer z3 = new Zimmer("zimmer3", 3);
		Zimmer z4 = new Zimmer("zimmer4", 3);
		Zimmer z5 = new Zimmer("zimmer4", 3);
		pm.saveEntity(z0);
		pm.saveEntity(z1);
		pm.saveEntity(z2);
		pm.saveEntity(z3);
		pm.saveEntity(z4);
		pm.saveEntity(z5);

		// ###### Reservierung

		List<Kunde> kundenlist0 = new ArrayList<Kunde>();
		kundenlist0.add(k0);
		kundenlist0.add(k1);
		kundenlist0.add(k2);
		kundenlist0.add(k3);
		kundenlist0.add(k4);
		kundenlist0.add(k5);

		List<Kunde> kundenlist1 = new ArrayList<Kunde>();
		kundenlist1.add(k0);
		kundenlist1.add(k1);
		kundenlist1.add(k2);
		kundenlist1.add(k3);

		List<Kunde> kundenlist2 = new ArrayList<Kunde>();
		kundenlist2.add(k0);

		List<Kunde> kundenlist3 = new ArrayList<Kunde>();
		kundenlist3.add(k0);
		kundenlist3.add(k1);

		Belegung b0 = new Belegung(kundenlist3, z3);
		Belegung b1 = new Belegung(kundenlist2, z2);

		List<Belegung> belegunglist0 = new ArrayList<Belegung>();
		belegunglist0.add(b0);

		List<Belegung> belegunglist1 = new ArrayList<Belegung>();
		belegunglist1.add(b1);

		Reservierung r0 = new Reservierung(belegunglist0, new Date("1 Jan 2012"), new Date("10 Jan 2012"));
		Reservierung r1 = new Reservierung(belegunglist1, new Date("1 Jan 2012"), new Date("5 Jan 2012"));
		
		new Rechnung(r1); // adds a rechnung to reservation1 
		
		this.pm.saveEntity(r0);
		this.pm.saveEntity(r1);

	}

	@Test
	public void testBeforeInit()
	{
		Assert.assertEquals(6, this.pm.getAllKunde().size());
		Assert.assertEquals(6, this.pm.getAllZimmer().size());
		Assert.assertNotNull(this.pm.getKundeByName("parent0", "nach0"));
	}

	@Test
	public void testFilter()
	{
		List<?> l0 = this.pm.filter(null, null, null, null, false);
		Assert.assertEquals(2, l0.size());
		List<?> l1 = this.pm.filter(new Date("1 Jan 2012"), null, null, null, false);
		Assert.assertEquals(2, l1.size());
		List<?> l2 = this.pm.filter(new Date("1 Jan 2012"), new Date("7 Jan 2012"), null, null, false);
		Assert.assertEquals(1, l2.size());
		List<?> l3 = this.pm.filter(new Date("1 Jan 2012"), new Date("12 Jan 2012"), null, null, false);
		Assert.assertEquals(2, l3.size());
		
		List<?> l4 = this.pm.filter(new Date("1 Jan 2012"), new Date("12 Jan 2012"), "ch0", null, false);
		Assert.assertEquals(2, l4.size());
		List<?> l5 = this.pm.filter(new Date("1 Jan 2012"), new Date("12 Jan 2012"), "nach1", null, false);
		Assert.assertEquals(1, l5.size());
		
		List<?> l6 = this.pm.filter(new Date("1 Jan 2012"), new Date("12 Jan 2012"), null, "zimmer3", false);
		Assert.assertEquals(1, l6.size());
		List<?> l7 = this.pm.filter(new Date("1 Jan 2012"), new Date("12 Jan 2012"), null, "zimmer0", false);
		Assert.assertEquals(0, l7.size());
		
		List<?> l8 = this.pm.filter(new Date("1 Jan 2012"), new Date("12 Jan 2012"), null, null, true);
		Assert.assertEquals(1, l8.size());

		System.out.println(l8);
	}
}
